package org.iteam.xlz.core.service.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.base.Parameters;

public class ParameterService extends BaseService<Parameters>{
	
	static ParameterService instance;
	
	private ParameterService(){}
	
	public static ParameterService getInstance(){
		if(null==instance){
			instance=new ParameterService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from parameters");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<Parameters> query(long page,long rows){
		try {
			return super.query("select * from parameters where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Parameters>();
		}
	}
	
	public Parameters queryByName(String name){
		try {
			return super.show("select * from parameters where 1=1 and name=?",name);
		} catch (SQLException e) {
			e.printStackTrace();
			return new Parameters();
		}
		
	}
	
	public boolean update(Parameters model){
		SqlBuilder builder=SqlBuilder.createUpdate("parameters");
		
		builder.addField("value",model.getValue());
		builder.addField("description",model.getDescription());
		builder.addWhere("name",model.getName());
		
		try {
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
